package servidores;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServidorDNS {
    List<ThreadClienteDNS> listaClientesDNS; 
    ServerSocket socketServerDNS;
    int puertoDNS;
            
    public ServidorDNS(int portDNS) {
        listaClientesDNS = new ArrayList<ThreadClienteDNS> ();
        this.puertoDNS = portDNS;
    }
    
    public void iniciar () throws IOException {
        socketServerDNS = new ServerSocket (puertoDNS);
        System.out.println("BackEnd-DNS: Servidor de DNS iniciado");
        while (true) {
            try {
            	Socket socketCliente = socketServerDNS.accept();
                ThreadClienteDNS cliente;
				cliente = new ThreadClienteDNS(socketCliente,listaClientesDNS);
				listaClientesDNS.add(cliente);
				cliente.start();
                System.out.println("BackEnd-DNS: Nuevo Cliente Conectado");
            }
            catch (IOException e) {
            	System.out.println("BackEnd-DNS: Error al iniciar le servidor de DNS");
            }
        }
    }

	public int getPuerto() {
		return puertoDNS;
	}

	public void setPuerto(int puerto) {
		this.puertoDNS = puerto;
	}
	
	public static void main(String args[]) {
    	ServidorDNS servidorDNS = new ServidorDNS(5002);
    	try {
			servidorDNS.iniciar();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
